//
//  JXBaseTool.m
//  JiaoX
//
//  Created by jiaoxiu－004 on 14/11/12.
//  Copyright (c) 2014年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXBaseTool.h"

@implementation JXBaseTool
+ (void)getWithUrl:(NSString *)url param:(NSDictionary *)param hasT:(BOOL)hasT resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
//    NSDictionary *mParams = [SignUtils getParams:[param keyValues] shouldLogin:hasT];
    NSDictionary *mParams = [SignUtils getParams:param shouldLogin:hasT];
    [JXHttpTool get:url params:mParams success:^(id responseObj) {
        if (success) {
            id result = [resultClass objectWithKeyValues:responseObj];
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)postWithUrl:(NSString *)url param:(NSDictionary *)param hasT:(BOOL)hasT resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    if(!param){
        param = [[[JXBaseRequest alloc]init] keyValues];
    }
    NSDictionary *mParams = [SignUtils getParams:param shouldLogin:hasT];
    [JXHttpTool post:url params:mParams success:^(id responseObj) {
        if (success) {
            JXLog(@"%@:%@",resultClass,[[NSString alloc]initWithData:responseObj encoding:NSUTF8StringEncoding]);
            id result = [resultClass objectWithJSONData:responseObj];
            [self checkErrorInfo:result];//判断是否过期
            success(result);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+(void)checkErrorInfo:(id)result{
    JXBaseResponse *response = (JXBaseResponse *)result;
    if(response.data){
        switch (response.code) {
            case 2://身份已过期 密钥不匹配,强制退出
                {
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"token_timeout" object:self];
                }
                break;
            case 5://登录后才能访问的接口
                {
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"token_timeout" object:self];
                }
                break;
            default:
           
                break;
        }
    }
}

@end
